//
//  FFRedPacketsSystemCell.m
//  LiMaoRedPackets
//
//  Created by tt on 2020/9/22.
//

#import "FFRedPacketsSystemCell.h"
#import "FFRedPacketsSystemContent.h"
#import "FFRedPacketsRecordVC.h"
@interface FFRedPacketsSystemCell ()

@property(nonatomic,strong) UILabel *tipTextLbl;


@property(nonatomic,strong) WKMessageModel *messageModel;

@end

@implementation FFRedPacketsSystemCell

#define LIM_REDPACKET_INSETS UIEdgeInsetsMake(4.0f, 4.0f, 4.0f, 4.0f) // 红包内边距

+ (CGSize)sizeForMessage:(WKMessageModel *)model {
    FFRedPacketsSystemContent *content = (FFRedPacketsSystemContent*)model.content;
    CGSize contentSize = [content displayContent2Size:[WKApp shared].config.systemMessageContentMaxWidth];
    
    CGFloat bottomSpace = 20.0f;
    return CGSizeMake(contentSize.width+LIM_REDPACKET_INSETS.left+LIM_REDPACKET_INSETS.right, contentSize.height+LIM_REDPACKET_INSETS.top+LIM_REDPACKET_INSETS.bottom + bottomSpace);
}

- (void)initUI {
    [super initUI];
    [self setBackgroundColor:[UIColor clearColor]];
  
//    [self.contentView addSubview:self.tipTextBoxView];
    [self.contentView addSubview:self.tipTextLbl];
    
//    [self.contentView addSubview:self.redpacketBtn];
//    [self.contentView addSubview:self.redpacketIconImgView];
    
}

- (void)refresh:(WKMessageModel *)model {
    [super refresh:model];
    self.messageModel = model;
    FFRedPacketsSystemContent *content = (FFRedPacketsSystemContent*)model.content;
    self.tipTextLbl.attributedText = content.displayContent2;
    
    self.tipTextLbl.backgroundColor = [WKApp shared].config.cellBackgroundColor;
    
    CGSize contentSize = [content displayContent2Size:[WKApp shared].config.systemMessageContentMaxWidth];
    self.tipTextLbl.lim_size = CGSizeMake(contentSize.width+LIM_REDPACKET_INSETS.left + LIM_REDPACKET_INSETS.right, contentSize.height + LIM_REDPACKET_INSETS.top + LIM_REDPACKET_INSETS.bottom);
    
    
}

- (UILabel *)tipTextLbl {
    if(!_tipTextLbl) {
        _tipTextLbl = [[UILabel alloc] init];
        [_tipTextLbl setTextAlignment:NSTextAlignmentCenter];
        [_tipTextLbl setFont:[[WKApp shared].config appFontOfSize:[WKApp shared].config.messageTipTimeFontSize]];
        [_tipTextLbl setTextColor:[UIColor grayColor]];
        [_tipTextLbl setNumberOfLines:0];
        _tipTextLbl.lineBreakMode = NSLineBreakByWordWrapping;
        _tipTextLbl.layer.masksToBounds = YES;
        _tipTextLbl.layer.cornerRadius = 10.0f;
        _tipTextLbl.userInteractionEnabled = YES;
        [_tipTextLbl addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTipText:)]];

    }
    return _tipTextLbl;
}

-(void) onTipText:(UITapGestureRecognizer*)gesture {
    if( [self.tipTextLbl didTapAttributedTextInLabel:gesture inRange:NSMakeRange(self.tipTextLbl.text.length-2, 2)]) {
        [self onRedpacket];
    }
}


-(void) onRedpacket {
    FFRedPacketsSystemContent *content = (FFRedPacketsSystemContent*)self.messageModel.content;
    // 跳转
    FFRedPacketsRecordVC *vc = [[FFRedPacketsRecordVC alloc] init];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    vc.redPacketsRecordType = self.messageModel.channel.channelType;
    vc.redpacket_no = content.redpacketNo;
    [[WKNavigationManager shared] pushViewController:vc animated:YES];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
//    self.tipTextBoxView.ff_size = CGSizeMake(contentSize.width-10.0f- LIM_REDPACKET_LEFT_SPACE - LIM_REDPACKET_RIGHT_SPACE, contentSize.height-10.0f);
//
//    self.tipTextBoxView.ff_left = self.ff_width/2.0f - self.tipTextBoxView.ff_width/2.0f;
//
    
    
    self.tipTextLbl.lim_centerX_parent = self.contentView;
//
//    self.redpacketIconImgView.ff_top = self.tipTextBoxView.ff_height/2.0f - self.redpacketIconImgView.ff_height/2.0f;
//    self.redpacketIconImgView.ff_left = self.tipTextBoxView.ff_left - self.redpacketIconImgView.ff_width - 5.0f;
//
//    self.redpacketBtn.ff_left = self.tipTextBoxView.ff_right + 2.0f;
//    self.redpacketBtn.ff_top = self.tipTextBoxView.ff_height/2.0f - self.redpacketBtn.ff_height/2.0f;
}

-(UIImage*) imageName:(NSString*)name {

    return [[WKApp shared] loadImage:name moduleID:@"LiMaoRedPackets"];
}

@end
